import {useCallback, useEffect, useRef} from "react"

const useThrottle: Function = (fn: Function, delay: number, dep: any[] = []) => {
    const {current} = useRef({fn, timer: undefined});
    useEffect(() => {
        current.fn = fn;
    }, [fn]);

    return useCallback(function (...args: any) {
        if (!current.timer) {
            // @ts-ignore
            current.timer = setTimeout(() => {
                delete current.timer;
            }, delay);
            // @ts-ignore
            return current.fn.call(this, ...args)
        }
    }, dep)
}
export default useThrottle;